/**
 * Copyright (c) 2012, Hakans Pirates - 
 * Johan Andersson spaiki17@gmail.com, 
 * Mattias Lundberg lundberg.mattias@gmail.com, 
 * Samuel Sjodin sjoodin@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pirates;

import com.jme3.system.AppSettings;
import com.jme3.system.JmeContext;
import pirates.network.AuthRegistror;
import pirates.notifications.Notify;

/**
 * Main application
 * 
 * @author mattias
 */
public class Main {

	public static void main(String[] args) {
		Util.setDebug(false); // sets the debug on/off of standard jme debug
								// messages
		Util.registerSerializers();// needs to be done before client/server
									// communication starts

		AppSettings settings = new AppSettings(true);
		settings.setFrameRate(Settings.FRAME_RATE);
		settings.setTitle(Settings.GAME_NAME);

		if (args.length > 0 && (args[0].contains("server"))) { // server code

			if (args != null && args.length > 1 && args[1].length() > 2) {
				Settings.HOST_NAME = args[1];
				Settings.IS_SERVER = true;
			} else {
				Notify.stdout("You need to specify a 2 or mor character server name.");
				return;
			}

			Notify.stdout("Starting in Headless / server mode!");

			if (args.length > 2 && args[2].contains("silent")) {
				Notify.stdout("Starting server in silent / unregistered mode.");
			} else {
				AuthRegistror auth = new AuthRegistror();
				if (Settings.HOST_NAME != null
						&& auth.register(Settings.HOST_NAME)) {
					Notify.stdout("Successfully registered server.");
				} else {
					Notify.stdout("Problems while registring server at auth.");
					return;
				}
			}
			PiratesServer app = new PiratesServer();
			app.setSettings(settings);
			app.start(JmeContext.Type.Headless);

		} else { // client code
			if (Settings.HOST_NAME == null) {
				if (args.length > 0) {
					Settings.HOST_NAME = args[0];
				} else {
					Notify.popup("You need to specify a player name.");
					return;
				}
			}
			if (Settings.SERVER_IP_ADDRESS == null) {
				if (args.length > 1) {
					Settings.SERVER_IP_ADDRESS = args[1];
				} else {
					Notify.popup("You need to specify a server ip address.");
					return;
				}
			}

			Pirates app = new Pirates();
			app.setSettings(settings);
			app.start();
		}
	}
}